package com.ddwl.gate.util;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;

/**
 * @Author hemiaojun
 * @Date 2021/1/4 11:47
 */
@Slf4j
public class WxaCodeUtil {

    public static String changeLogo(File wxaCode,File logoFile,File newWxaCode,int width,int height){
        //截取为正方形图片,长宽自定义，我自己估摸着是195，这个可以根据自己情况自己设置大小
        File tempFile =null;
        try {
            tempFile = File.createTempFile("logo_temp", ".png");
            //此处是要替代logo的自定义图片路径，自定义图片不要用透明的或者半透明的
            BufferedImage image = ImageIO.read(logoFile);
            BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = output.createGraphics();
            output = g2.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            g2.dispose();
            g2 = output.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.fillRoundRect(0, 0,width, height, 720, 720);
            g2.setComposite(AlphaComposite.SrcIn);
            g2.drawImage(image, 0, 0, width, height, null);
            g2.dispose();
            //这是生成的临时替换logo图片的保存路径
            ImageIO.write(output, "png", tempFile);
            //此处是小程序码的路径
            BufferedImage appletImg = ImageIO.read(new FileInputStream(wxaCode));
            Graphics2D g2d = appletImg.createGraphics();
            // 设置抗锯齿的属性
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            //此处是替换logo的临时图片路径
            BufferedImage centerImg = ImageIO.read(tempFile);
            g2d.drawImage(centerImg.getScaledInstance(centerImg.getWidth(), centerImg.getHeight(), Image.SCALE_SMOOTH), (appletImg.getWidth() - centerImg.getWidth()) / 2, (appletImg.getHeight() - centerImg.getHeight()) / 2, null);
            // 关闭资源
            g2d.dispose();
            //生成新的二维码，覆盖原来的，此处为原小程序码路径，如需另为保存，请自定义路径
            ImageIO.write(appletImg, "png", newWxaCode);
        }catch (Exception e){
            log.error("小程序码logo替换出错：{}", e.getMessage());
        }finally {
            tempFile.delete();
        }
        return getImageBinary(newWxaCode);
    }

    public static String compoundShopCodeImage(File returnFile,File wxCodeFile) {
        try {
            //1、获取主图片
            BufferedImage big = ImageIO.read(WxaCodeUtil.class.getClassLoader().getResourceAsStream("image/shopCodeBox.jpg"));
            Graphics2D g2d=big.createGraphics();
            // 设置抗锯齿的属性
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            //2、拿到二维码
            BufferedImage wxCodeImg = ImageIO.read(wxCodeFile);
            g2d.drawImage(wxCodeImg,(big.getWidth()-wxCodeImg.getWidth())/2,312,null);
            g2d.dispose();
            ImageIO.write(big, "png", returnFile);
        } catch (Exception e) {
            log.error("合成店铺码出错{}",e.getMessage(),e);
        }
        return getImageBinary(returnFile);
    }

    static String getImageBinary(File file){
        BufferedImage bi;
        try {
            bi = ImageIO.read(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", baos);
            byte[] bytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(bytes).trim();
        } catch (IOException e) {
            log.error("文件流转字节流出错：{}", e.getMessage());
        }
        return null;
    }

    public static void byteToImage(File file,byte[] bytes){
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            BufferedImage bi1 =ImageIO.read(bais);
            ImageIO.write(bi1, "png", file);
        } catch (IOException e) {
            log.error("字节流转文件出错：{}", e.getMessage());
        }
    }

}
